<template>
  <a-modal v-model:visible="visible" :width="1000" title="选择" @ok="handleOk" :footer="null">
    <a-form class="smart-query-form" v-privilege="'oa:enterprise:query'">
      <a-row class="smart-query-form-row">
        <a-form-item label="应用名称" class="smart-query-form-item">
          <a-input style="width: 200px" v-model:value="queryForm.appName" placeholder="请输入应用名称" />
        </a-form-item>
        <!-- <a-form-item label="协议" class="smart-query-form-item">
                    <a-select v-model:value="queryForm.protocol" style="width: 300px" placeholder="请选择协议"
                        :options="protocolOption"></a-select>
                </a-form-item> -->
        <a-form-item label="分类" class="smart-query-form-item">
          <a-select v-model:value="queryForm.category" style="width: 200px" placeholder="请选择协议" :options="FindList"></a-select>
        </a-form-item>

        <a-form-item class="smart-query-form-item smart-margin-left10">
          <a-button-group>
            <a-button type="primary" @click="onSearch">
              <template #icon>
                <SearchOutlined />
              </template>
              查询
            </a-button>
            <a-button @click="resetQuery">
              <template #icon>
                <ReloadOutlined />
              </template>
              重置
            </a-button>
          </a-button-group>
          <a-button type="primary" @click="tableSelectChange" style="margin-left: 10px"> 确定 </a-button>
        </a-form-item>
      </a-row>
    </a-form>
    <a-table
      size="small"
      rowKey="appName"
      :dataSource="tableData"
      :columns="columns"
      :pagination="false"
      :loading="tableLoading"
      bordered
      :row-selection="{ selectedRowKeys: selectedRowKeyList, onChange: onSelectChange, type: 'radio' }"
    >
      <template #bodyCell="{ column, record, text, index }">
        <template v-if="column.dataIndex === 'index'">
          {{ index }}
        </template>
        <template v-if="column.dataIndex === 'iconBase64'">
          <img :src="record.iconBase64" alt="" style="width: 40px" />
        </template>
        <template v-if="column.dataIndex === 'status'">
          <a-switch v-model:checked="record.status" :checkedValue="1" :unCheckedValue="0" :disabled="true">
            <template #checkedChildren><check-outlined /></template>
            <template #unCheckedChildren><close-outlined /></template>
          </a-switch>
        </template>
        <template v-if="column.dataIndex === 'category'">
          {{ FindList.filter((i) => i.value === record.category).length ? FindList.filter((i) => i.value === record.category)[0].label : '' }}
        </template>
      </template>
    </a-table>

    <div class="smart-query-table-page">
      <a-pagination
        showSizeChanger
        showQuickJumper
        show-less-items
        :pageSizeOptions="PAGE_SIZE_OPTIONS"
        :defaultPageSize="queryForm.pageSize"
        v-model:current="queryForm.pageNumber"
        v-model:pageSize="queryForm.pageSize"
        :total="total"
        @change="ajaxQuery"
        @showSizeChange="ajaxQuery"
        :show-total="(total) => `共${total}条`"
      />
    </div>
  </a-modal>
</template>
<script setup>
import { reactive, ref, onMounted } from 'vue';
import { message, Modal } from 'ant-design-vue';
import { PAGE_SIZE, PAGE_SIZE_OPTIONS } from '/@/constants/common-const';
import { useRouter } from 'vue-router';
import { smartSentry } from '/@/lib/smart-sentry';
import { applicationManagementApi } from '/@/api/system/applicationManagement-api';

const visible = ref(false);
function showModal() {
  visible.value = true;
}
defineExpose({
  showModal,
});
const props = defineProps({
  value: [Number, Array],
});

const emit = defineEmits(['update:value'], 'change');
// --------------------------- 企业表格 列 ---------------------------
const addType = ref([
  { name: 'OAuth2.x' },
  { name: 'SAML2.0' },
  { name: 'CAS认证' },
  { name: 'JWT令牌' },
  { name: '令牌认证' },
  { name: '表单认证' },
  { name: 'API扩展认证' },
  { name: '基本登录' },
]);
const statusOptions = ref([
  { label: 'ACTIVE', value: 1 },
  { label: 'INACTIVE', value: 2 },
  { label: 'DISABLED', value: 4 },
  { label: 'LOCK', value: 5 },
  { label: 'UNLOCK', value: 6 },
  { label: 'DELETE', value: 9 },
]);
const columns = ref([
  {
    title: '序号',
    dataIndex: 'index',
    width: 50,
    align: 'center',
  },
  {
    title: '图标',
    dataIndex: 'iconBase64',
  },
  {
    title: '应用名称',
    dataIndex: 'appName',
  },
  {
    title: '协议',
    dataIndex: 'protocol',
  },
  {
    title: '分类',
    dataIndex: 'category',
    // fixed: 'right',
  },
  {
    title: '排序',
    dataIndex: 'sortIndex',
  },
  {
    title: '状态',
    dataIndex: 'status',
    // fixed: 'right',
  },
]);

const protocolOption = ref([
  { label: 'ALL', value: '' },
  { label: 'OAuth_v2.0', value: 'OAuth_v2.0' },
  { label: 'OAuth_v2.1', value: 'OAuth_v2.1' },
  { label: 'OpenID Connect v1.0', value: 'OpenID Connect v1.0' },
  { label: 'SAML v2.0', value: 'SAML v2.0' },
  { label: 'CAS', value: 'CAS' },
  { label: 'JWT', value: 'JWT' },
  { label: 'Token Based', value: 'Token Based' },
  { label: 'Form Based', value: 'Form Based' },
  { label: 'Extend API', value: 'Extend API' },
  { label: 'Basic', value: 'Basic' },
]);
// --------------------------- 查询 ---------------------------

const queryFormState = {
  pageNumber: 1,
  pageSize: PAGE_SIZE,
  searchCount: true,
  appName: '',
  category: '',
  protocol: '',
};
const queryForm = reactive({ ...queryFormState });
const tableLoading = ref(false);
const tableData = ref([]);
const total = ref(0);

// 日期选择
const searchDate = ref();

function dateChange(dates, dateStrings) {
  console.log(dateStrings);
  queryForm.startTime = dateStrings[0];
  queryForm.endTime = dateStrings[1];
}
// 查询
function onSearch() {
  queryForm.pageNumber = 1;
  ajaxQuery();
}
// 重置查询
function resetQuery() {
  searchDate.value = [];
  Object.assign(queryForm, queryFormState);
  ajaxQuery();
}
function dateToSeconds(dateStr) {
  if (dateStr == '') {
    return '';
  }
  let date = new Date(dateStr);
  return date.getTime() / 1000;
}
// 列表数据获取
async function ajaxQuery() {
  try {
    tableLoading.value = true;
    let responseModel = await applicationManagementApi.getFetch(queryForm);
    const list = responseModel.data.rows.map((i, index) => {
      i['key'] = index;
      return i;
    });
    total.value = responseModel.data.records;
    tableData.value = list;
  } catch (e) {
    smartSentry.captureError(e);
  } finally {
    tableLoading.value = false;
  }
}

// 获取分类下拉
const FindList = ref([]);
async function getFindList() {
  try {
    let res = await applicationManagementApi.queryFindList();
    FindList.value = res.data.map((item) => {
      return {
        label: item.name,
        value: item.id,
      };
    });
    FindList.value.unshift({ label: 'ALL', value: '' });
  } catch (e) {
    smartSentry.captureError(e);
  } finally {
  }
}
// 选择表格行
const selectedRowKeyList = ref([]);
const records = ref([]);
function onSelectChange(selectedRowKeys, record) {
  selectedRowKeyList.value = selectedRowKeys;
  records.value = record;
}
function tableSelectChange() {
  emit('update:value', selectedRowKeyList.value);
  emit('change', records.value);
  visible.value = false;
}
onMounted(() => {
  ajaxQuery();
//   getFindList();
});
</script>
<style scoped></style>
